﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cos.Core.Model.Template;

namespace Cos.Core.NHibernate.Util
{
    public static class TemplateModelConverter
    {
        public static TemplateDataModel ToTemplateDataModel(this Template template)
        {
            var templateDataModel = new TemplateDataModel
            {
                TemplateName = template.TemplateName,
                IsListView = template.IsListView,
                TemplateUrl = template.TemplateUrl
            };

            return templateDataModel;
        }

        public static TemplateDataModel ToTemplateDataModel(this TemplateDataModel templateDataModel, Template template)
        {
            if (templateDataModel == null)
            {
                return ToTemplateDataModel(template);
            }

            templateDataModel.TemplateName = template.TemplateName;
            templateDataModel.IsListView = template.IsListView;
            templateDataModel.TemplateUrl = template.TemplateUrl;

            return templateDataModel;
        }

        public static Template ToTemplate(this TemplateDataModel templateDataModel)
        {
            var model = new Template
            {
                TemplateId = templateDataModel.Id,
                TemplateName = templateDataModel.TemplateName,
                TemplateUrl = templateDataModel.TemplateUrl,
                IsListView = templateDataModel.IsListView
            };

            return model;
        }

        public static PartDataModel ToPartDataModel(this Part part)
        {
            var partDataModel = new PartDataModel
                    {
                        PartName = part.PartName,
                        DataTypeId = part.DataTypeId,
                        PartOrder = part.PartOrder,
                        IsMultiCulture = part.IsMultiCulture,
                        IsRequired = part.IsRequired,
                        MinValue = part.MinValue,
                        MaxValue = part.MaxValue,
                        MinLength = part.MinLength,
                        MaxLength = part.MaxLength,
                        CustomValidator = part.CustomValidator,
                        ThumbnailWidth = part.ThumbnailWidth,
                        ThumbnailHeight = part.ThumbnailHeight,
                        PresentationWidth = part.PresentationWidth,
                        PresentationHeight = part.PresentationHeight,
                        TemplateId = part.TemplateId
                    };

            return partDataModel;
        }

        public static PartDataModel ToPartDataModel(this PartDataModel partDataModel, Part part)
        {
            if (partDataModel == null)
            {
                return ToPartDataModel(part);
            }

            partDataModel.PartName = part.PartName;
            partDataModel.DataTypeId = part.DataTypeId;
            partDataModel.PartOrder = part.PartOrder;
            partDataModel.IsMultiCulture = part.IsMultiCulture;
            partDataModel.IsRequired = part.IsRequired;
            partDataModel.MinValue = part.MinValue;
            partDataModel.MaxValue = part.MaxValue;
            partDataModel.MinLength = part.MinLength;
            partDataModel.MaxLength = part.MaxLength;
            partDataModel.CustomValidator = part.CustomValidator;
            partDataModel.ThumbnailWidth = part.ThumbnailWidth;
            partDataModel.ThumbnailHeight = part.ThumbnailHeight;
            partDataModel.PresentationWidth = part.PresentationWidth;
            partDataModel.PresentationHeight = part.PresentationHeight;
            partDataModel.TemplateId = part.TemplateId;

            return partDataModel;
        }
        
        public static Part ToPart(this PartDataModel partDataModel)
        {
            var part = new Part
            {
                Id = partDataModel.Id,
                PartName = partDataModel.PartName,
                DataTypeId = partDataModel.DataTypeId,
                PartOrder = partDataModel.PartOrder,
                IsMultiCulture = partDataModel.IsMultiCulture,
                IsRequired = partDataModel.IsRequired,
                MinValue = partDataModel.MinValue,
                MaxValue = partDataModel.MaxValue,
                MinLength = partDataModel.MinLength,
                MaxLength = partDataModel.MaxLength,
                CustomValidator = partDataModel.CustomValidator,
                ThumbnailWidth = partDataModel.ThumbnailWidth,
                ThumbnailHeight = partDataModel.ThumbnailHeight,
                PresentationWidth = partDataModel.PresentationWidth,
                TemplateId = partDataModel.TemplateId
            };

            return part;
        }

        public static OptionDataModel ToOptionDataModel(this Option option)
        {
            var optionDataModel = new OptionDataModel
                                      {
                                          OptionName = option.Name,
                                          PartId = option.PartId,
                                          OptionOrder = option.Order
                                      };

            return optionDataModel;
        }

        public static OptionDataModel ToOptionDataModel(this OptionDataModel optionDataModel, Option option)
        {
            if (optionDataModel == null)
            {
                return ToOptionDataModel(option);
            }

            optionDataModel.OptionName = option.Name;
            optionDataModel.PartId = option.PartId;
            optionDataModel.OptionOrder = option.Order;

            return optionDataModel;
        }

        public static Option ToOption(this OptionDataModel optionDataModel)
        {
            var option = new Option
                             {
                                 Name = optionDataModel.OptionName,
                                 PartId = optionDataModel.PartId,
                                 Order = optionDataModel.OptionOrder
                             };

            return option;
        }

        public static DataTypeDataModel ToDataTypeDataModel(this DataType dataType)
        {
            var dataTypeDataModel = new DataTypeDataModel
                                        {
                                            DataTypeName = dataType.Name,
                                            HasMedia = dataType.HasMedia,
                                            IsFormField = dataType.IsFormField
                                        };

            return dataTypeDataModel;
        }

        public static DataTypeDataModel ToDataTypeDataModel(this DataTypeDataModel dataTypeDataModel, DataType dataType)
        {
            if (dataTypeDataModel == null)
            {
                return ToDataTypeDataModel(dataType);
            }

            dataTypeDataModel.DataTypeName = dataType.Name;
            dataTypeDataModel.HasMedia = dataType.HasMedia;
            dataTypeDataModel.IsFormField = dataType.IsFormField;

            return dataTypeDataModel;
        }

        public static DataType ToDataType(this DataTypeDataModel dataTypeDataModel)
        {
            var dataType = new DataType
                               {
                                   Id = dataTypeDataModel.Id,
                                   Name = dataTypeDataModel.DataTypeName,
                                   HasMedia = dataTypeDataModel.HasMedia,
                                   IsFormField = dataTypeDataModel.IsFormField
                               };

            return dataType;
        }
    }
}
